package hbt;

import modelo.Cliente;


import org.hibernate.Session;
import org.hibernate.SessionFactory;


public class HibernateClienteDAO {
	private static HibernateClienteDAO instancia = null;
	private static SessionFactory sf = null;

	public static HibernateClienteDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new HibernateClienteDAO();
		} 
		return instancia;
	}
	
	public void grabarCliente(Cliente cli){
		Session session = sf.openSession();
		session.beginTransaction();
		session.persist(cli);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarCliente(Cliente cli) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(cli);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarCliente(Cliente cli) {
		Session session = sf.openSession();
		session.beginTransaction();
		session.merge(cli);
		session.flush();
		session.getTransaction().commit();
		session.close();
		
	}
	
	public Cliente consultarCliente(int idcli) {
		Session session = sf.openSession();
		session.beginTransaction();
		Cliente cli = (Cliente) session.get(Cliente.class, idcli);
		return cli;
		
	}
}
